package org.example;


import java.awt.*;
import java.util.ArrayList;
import java.util.Random;

public class Snake {
    private ArrayList<Point> body;
    private SnakeGame.Direction direction;
    private SnakeGame.Direction nextDirection;
    private int tileSize;
    private boolean alive = true; // 添加存活状态
    private int skinId;
    private static final Color[][] SKINS = {
            // 1. 绿色渐变（默认）
            {new Color(100, 200, 100), new Color(80, 180, 80), new Color(60, 160, 60), new Color(40, 140, 40)},
            // 2. 蓝色渐变
            {new Color(50, 150, 255), new Color(40, 130, 230), new Color(30, 110, 205), new Color(20, 90, 180)},
            // 3. 紫色渐变
            {new Color(180, 100, 255), new Color(160, 80, 230), new Color(140, 60, 205), new Color(120, 40, 180)},
            // 4. 橙色渐变
            {new Color(255, 180, 50), new Color(230, 160, 40), new Color(205, 140, 30), new Color(180, 120, 20)},
            // 5. 霓虹渐变
            {new Color(255, 50, 255), new Color(100, 50, 255), new Color(50, 150, 255), new Color(50, 255, 200)},
            // 6. 粉色渐变
            {new Color(255, 150, 200), new Color(255, 130, 180), new Color(255, 110, 160), new Color(255, 90, 140)},
            // 7. 金色渐变
            {new Color(255, 215, 0), new Color(238, 198, 0), new Color(221, 181, 0), new Color(204, 164, 0)},
            // 8. 暗色渐变
            {new Color(180, 180, 180), new Color(150, 150, 150), new Color(120, 120, 120), new Color(90, 90, 90)}
    };

    // 标记蛇死亡
    public void die() {
        this.alive = false;
    }

    // 检查蛇是否存活
    public boolean isAlive() {
        return alive;
    }

    public Snake(int startGridX, int startGridY, int tileSize, int skinId) {
        this.tileSize = tileSize;
        body = new ArrayList<>();
        this.skinId = skinId;

        // 初始化蛇身（3个部分）- 使用网格坐标
        Color[] skin = SKINS[this.skinId];
        body.add(new Point(startGridX, startGridY, skin[0])); // 头
        body.add(new Point(startGridX - 1, startGridY, skin[1])); // 身体1
        body.add(new Point(startGridX - 2, startGridY, skin[2])); // 身体2

        direction = SnakeGame.Direction.RIGHT;
        nextDirection = SnakeGame.Direction.RIGHT;
    }
    public void grow() {
        Color[] skin = SKINS[skinId];
        Point tail = body.get(body.size() - 1);
        body.add(new Point(tail.getGridX(), tail.getGridY(), skin[3])); // 使用皮肤的第四种颜色
    }
    public void move(int j) {
        direction = nextDirection;

        // 移动身体（从尾部开始）
        for (int i = body.size() - 1; i > 0; i--) {
            body.get(i).setGridX(body.get(i-1).getGridX());
            body.get(i).setGridY(body.get(i-1).getGridY());
        }

        // 移动头部
        Point head = body.get(0);
        switch(direction) {
            case UP:
                head.setGridY(head.getGridY() - j);
                break;
            case DOWN:
                head.setGridY(head.getGridY() + j);
                break;
            case LEFT:
                head.setGridX(head.getGridX() - j);
                break;
            case RIGHT:
                head.setGridX(head.getGridX() + j);
                break;
        }
    }


    public void shrink() {
        if(body.size() > 1)
            body.remove(body.size() - 1);
        else
            die();
    }

    public void setDirection(SnakeGame.Direction newDirection) {
        // 防止180度转向
        if ((direction == SnakeGame.Direction.UP && newDirection != SnakeGame.Direction.DOWN) ||
                (direction == SnakeGame.Direction.DOWN && newDirection != SnakeGame.Direction.UP) ||
                (direction == SnakeGame.Direction.LEFT && newDirection != SnakeGame.Direction.RIGHT) ||
                (direction == SnakeGame.Direction.RIGHT && newDirection != SnakeGame.Direction.LEFT)) {
            nextDirection = newDirection;
        }
    }

    public Point getHead() {
        return body.get(0);
    }

    public boolean checkSelfCollision() {
        Point head = getHead();
        // 检查头部是否与身体其他部分碰撞
        for (int i = 1; i < body.size(); i++) {
            if (head.equals(body.get(i))) {
                return true;
            }
        }
        return false;
    }

    public void draw(Graphics2D g) {
        if (!alive)
            return;
        //绘制身体
        for (int i = 0; i < body.size(); i++) {
            Point segment = body.get(i);
            g.setColor(segment.getColor());

            int pixelX = segment.getGridX() * tileSize;
            int pixelY = segment.getGridY() * tileSize;

            g.fillRoundRect(pixelX, pixelY, tileSize, tileSize, 5, 5);

            // 绘制头部眼睛
            if (i == 0) {
                g.setColor(Color.BLACK);
                int eyeSize = tileSize / 5;
                int offset = tileSize / 4;

                // 根据方向绘制眼睛位置
                switch(direction) {
                    case UP:
                        g.fillOval(pixelX + offset, pixelY + offset, eyeSize, eyeSize);
                        g.fillOval(pixelX + tileSize - offset - eyeSize, pixelY + offset, eyeSize, eyeSize);
                        break;
                    case DOWN:
                        g.fillOval(pixelX + offset, pixelY + tileSize - offset - eyeSize, eyeSize, eyeSize);
                        g.fillOval(pixelX + tileSize - offset - eyeSize, pixelY + tileSize - offset - eyeSize, eyeSize, eyeSize);
                        break;
                    case LEFT:
                        g.fillOval(pixelX + offset, pixelY + offset, eyeSize, eyeSize);
                        g.fillOval(pixelX + offset, pixelY + tileSize - offset - eyeSize, eyeSize, eyeSize);
                        break;
                    case RIGHT:
                        g.fillOval(pixelX + tileSize - offset - eyeSize, pixelY + offset, eyeSize, eyeSize);
                        g.fillOval(pixelX + tileSize - offset - eyeSize, pixelY + tileSize - offset - eyeSize, eyeSize, eyeSize);
                        break;
                }
            }
        }
    }

    public ArrayList<Point> getBody() {
        return body;
    }
}